home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / ASSEMBLE / 0938.ZIP / KEGELUNX.ARC / CAT.ASM < prev    next >
Assembly Source File  |  1986-05-07  |  4KB  |  217 lines

  1. page 66, 132
  2. ;---------- cat.com ------------
  3. ; Copies the named files to stdout.
  4. ; If no files named, uses stdout.
  5. ; Wildcards okay.
  6. ; Ensures that resulting file has no EOF marks.
  7. ; Complains if file not found.
  8.  
  9.  
  10. ; uses args
  11.     extrn    _args:near,    _shift:near
  12.     extrn    argc:word,    argv:word
  13.  
  14. ; uses ffind
  15.     extrn    ffirst:near,    fnext:near
  16.     extrn    fnam:byte
  17.  
  18. ; uses fio
  19.     extrn    f_io_init:near
  20.     extrn    f_ihand:word,    f_ohand:word
  21.     extrn    f_ibuf:word,    f_obuf:word
  22.     extrn    f_ilen:word,    f_olen:word
  23.     extrn    f_ibufend:word,    f_obufend:word
  24.     extrn    f_imore:near,    f_omore:near
  25.     extrn    f_getc:near,    f_putc:near
  26.  
  27. dos    macro    fn
  28.     mov    ah, fn
  29.     int    21h
  30.     endm
  31.  
  32. stdin    equ    0
  33. stdout    equ    1
  34. stderr    equ    2
  35.  
  36. code    segment para public 'CODE'
  37. assume cs:code,ds:code
  38.  
  39.     org    100h
  40. main    proc
  41.     jmp    cat
  42. main    endp
  43.  
  44.  
  45. ;----------------------------------------------
  46.  
  47.     db    27
  48.     db    '[2J cat (C) 1984 Yoyodyne, Inc- a growing excited company', 26
  49.  
  50. errs        dw    null        ; error zero- no error
  51.         dw    null, filenotfound, pathnotfound, nohand, access
  52.         dw    null, null, null, null, null
  53.         dw    null, null, null, null, baddrive
  54.         dw    null, null
  55.  
  56. null        db    '?dos err', 0
  57. filenotfound    db    'File not found', 0
  58. pathnotfound    db    'Path not found', 0
  59. nohand        db    'No handles left', 0
  60. access        db    'Access denied', 0
  61. baddrive    db    'Invalid drive specification', 0
  62.  
  63. trueargc    dw    ?
  64.  
  65. ;--- strlen ---
  66. ; Returns length of ES:DI in CX, points to null with DI.
  67.  
  68. strlen    proc    near
  69.     cld
  70.     push    ax
  71.     mov    cx, -1
  72.     mov    al, 0
  73.     repnz    scasb
  74.     not    cx
  75.     pop    ax
  76.     ret
  77. strlen    endp
  78.  
  79. dos_err    proc    near
  80.     mov    bx, ax
  81.     add    bx, ax
  82.     mov    di, errs[bx]
  83.     push    di
  84.     call    strlen
  85.     pop    dx
  86.     mov    bx, stderr
  87.     DOS    40h
  88.     ret
  89. dos_err    endp
  90.  
  91. open_err    proc    near
  92.     call    dos_err
  93.     mov    al, 1
  94.     DOS    4ch
  95. open_err    endp
  96.  
  97. ;-------------------------------------------------
  98.  
  99. cat    proc    near
  100.  
  101.     call    _args
  102.  
  103.     ; Initialize the buffers...
  104.     mov    f_ilen, 16384
  105.     mov    f_olen, 16384
  106.     call    f_io_init
  107.  
  108.     ; Get them args...
  109.     mov    ax, argc
  110.     mov    trueargc, ax    ; save for end of repeat loop
  111.     or    ax, ax
  112.     jnz    while_loop
  113.         ; If no arguments, use stdin.
  114.         mov    argc, 1
  115.         mov    bx, stdin
  116.         DOS    45h        ; get another handle to stdin
  117.         jc    open_err
  118.         mov    f_ihand, ax
  119.         jmp    short gothandle
  120.  
  121.     ;----- Main Loop -------
  122.     ; do {
  123.     ;   find_first_matching(argv[2]);
  124.     ;   repeat
  125.     ;       copy file to stdout;
  126.     ;       until not find_next;
  127.     ;   shift;
  128.     ;   } while (argc > 0);
  129.  
  130. while_loop:
  131.     mov    dx, argv[2]
  132.     mov    cx, 0
  133.     call    ffirst
  134.     jnc    rept_loop
  135.     call    notfound
  136.     jmp    rept_done    ; go on to next argument
  137.  
  138. rept_loop:
  139.     mov    dx, offset fnam
  140.     mov    al, 0        ; open for read access
  141.     dos    3dh
  142.     jc    open_err
  143.  
  144. gothandle:
  145.     mov    f_ihand, ax
  146.     mov    f_ohand, stdout
  147.  
  148.     ; SI & DI are used by getc & putc.
  149.     mov    si, f_ibuf
  150.     mov    f_ibufend, si
  151.     mov    di, f_obuf
  152.  
  153. moreclp:
  154.     call    f_getc
  155.     jz    morecx
  156.     cmp    al, 26
  157.     jz    morecx    
  158.     call    f_putc
  159.     jmp    moreclp
  160.  
  161. morecx:    call    f_omore        ; flush output buffer
  162.  
  163.     mov    bx, f_ihand
  164.     DOS    3Eh        ; close input file
  165.  
  166.     ;       until (not find_next);
  167.     cmp    trueargc, 0    ; no searching to do at all in this case
  168.     jz    while_done
  169.  
  170.     call    fnext
  171.     jc    rept_done
  172.     jmp    rept_loop
  173. rept_done:
  174.     ;   shift;
  175.     call    _shift
  176.     dec    argc
  177.     cmp    argc, 0
  178.     jz    while_done
  179.     jmp    while_loop
  180.  
  181. while_done:
  182.     ;mov    bx, f_ohand
  183.     ;DOS    3eh        ; close output file (don't close stdout?)
  184.  
  185.     mov    al, 0        ; no error
  186. exit:
  187.     mov    ah, 4ch
  188.     int    21h        ; terminate process, return status in AL.
  189.  
  190. notfound:
  191.     mov    dx, offset cantfind
  192.     DOS    9
  193.  
  194.     mov    di, argv[2]    ; get pointer to string
  195.     push    di
  196.     call    strlen
  197.     pop    dx
  198.     mov    bx, stdout
  199.     mov    ah, 40h
  200.     int    21h        ; print
  201.  
  202.     mov    dx, offset couldntfind
  203.     DOS    9
  204.     ret
  205. cat    endp
  206. cantfind:    db    "?cat: can't open $"
  207. couldntfind:    db    "."
  208.         db    0dh, 0ah
  209.         db    "$"
  210. code    ends
  211.  
  212.     end    main
  213.  
  214.  
  215.  
  216.  
  217.